package tcm.com.gistone.api.restController;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tcm.com.gistone.database.config.GetBySqlMapper;
import tcm.com.gistone.util.ClientUtil;
import tcm.com.gistone.util.EdatResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/10/12.
 */
@RestController("apiVoteController")
@RequestMapping("api/vote")
public class VoteController {


    @Autowired
    private GetBySqlMapper getBySqlMapper;


    /**
     * 设置用户对关键词点赞状态，并返回该关键词的点赞和点踩总数
     * status 1 踩 2 赞
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(value = "setWordVote", method = RequestMethod.POST)
    public EdatResult setWordVote(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            int status = data.getInt("status");
            String username = data.getString("username");
            String word = data.getString("word");
            Integer specialId = null;
            if (data.has("specialId")) {
                specialId = data.getInt("specialId");

            }

            System.out.println(specialId);
            String sql = "select * from tb_upvote_word where username ='" + username + "' and word ='" + word + "' ";
           if(null!=specialId){
               sql =sql+" and  special_id="+specialId;
           }
            List<Map> result = getBySqlMapper.findRecords(sql);

            if(result.size()>0){
                Object id =result.get(0).get("id");
                sql ="UPDATE tb_upvote_word set  status = "+status+" where id ="+id;
                getBySqlMapper.update(sql);
            }else{
                sql ="insert into tb_upvote_word(word,username,status,special_id) values ('" + word + "','" + username + "',"+status+","+specialId+")";
                getBySqlMapper.insert(sql);
            }
            sql ="select count(*) from tb_upvote_word where status =1 and word ='" + word + "' ";
            if(null!=specialId){
                sql =sql+" and  special_id="+specialId;
            }
            int downvote = getBySqlMapper.findrows(sql);
            sql ="select count(*) from tb_upvote_word where status =2 and word ='" + word + "' ";
            if(null!=specialId){
                sql =sql+" and  special_id="+specialId;
            }
            int upvote = getBySqlMapper.findrows(sql);
            Map map =new HashMap();
            map.put("downvote",downvote);
            map.put("upvote",upvote);
            return EdatResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }

    /**
     * 获取用户对关键词点赞状态，并返回该关键词的点赞和点踩总数
     * @param request
     * @param response
     * @return
     */

    @Transactional
    @RequestMapping(value = "getWordVote", method = RequestMethod.POST)
    public EdatResult getWordVote(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            String username = data.getString("username");
            String word = data.getString("word");
            Integer specialId = null;
            if (data.has("specialId")) {
                specialId = data.getInt("specialId");

            }
            String sql = "select * from tb_upvote_word where username ='" + username + "' and word ='" + word + "' ";
            if(null!=specialId){
                sql =sql+" and  special_id="+specialId;
            }
            List<Map> result = getBySqlMapper.findRecords(sql);
            int status=0;
            if(result.size()>0){
                status =(Integer) result.get(0).get("status");

            }
            sql ="select count(*) from tb_upvote_word where status =1 and word ='" + word + "' ";
            if(null!=specialId){
                sql =sql+" and  special_id="+specialId;
            }
            int downvote = getBySqlMapper.findrows(sql);
            sql ="select count(*) from tb_upvote_word where status =2 and word ='" + word + "' ";
            if(null!=specialId){
                sql =sql+" and  special_id="+specialId;
            }
            int upvote = getBySqlMapper.findrows(sql);
            Map map =new HashMap();
            map.put("status",status);
            map.put("downvote",downvote);
            map.put("upvote",upvote);
            return EdatResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }



    /**
     * 设置用户对段落点赞状态，并返回该关键词的点赞和点踩总数
     * status 1 踩 2 赞
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(value = "setSectionVote", method = RequestMethod.POST)
    public EdatResult setSectionVote(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            int status = data.getInt("status");
            String word = data.getString("word");
            String username = data.getString("username");
            long section = data.getLong("sectionId");
            String sql = "select * from tb_upvote_section where username ='" + username + "' and word ='" + word + "' and section ='" + section + "' ";
            List<Map> result = getBySqlMapper.findRecords(sql);

            if(result.size()>0){
                Object id =result.get(0).get("id");
                sql ="UPDATE tb_upvote_section set  status = "+status+" where id ="+id;
                getBySqlMapper.update(sql);
            }else{
                sql ="insert into tb_upvote_section(section,username,word,status) values (" + section + ",'" + username + "','" + word + "',"+status+")";
                getBySqlMapper.insert(sql);
            }
            sql ="select count(*) from tb_upvote_section where status =1 and word ='" + word + "' and section =" + section ;

            int downvote = getBySqlMapper.findrows(sql);
            sql ="select count(*) from tb_upvote_section where status =2  and word ='" + word + "' and section =" + section ;
            int upvote = getBySqlMapper.findrows(sql);
            Map map =new HashMap();
            map.put("downvote",downvote);
            map.put("upvote",upvote);
            return EdatResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }

    /**
     * 获取用户对段落点赞状态，并返回该关键词的点赞和点踩总数
     * @param request
     * @param response
     * @return
     */

    @Transactional
    @RequestMapping(value = "getSectionVote", method = RequestMethod.POST)
    public EdatResult getSectionVote(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            String username = data.getString("username");
            String word = data.getString("word");
            long section = data.getLong("sectionId");
            String sql = "select * from tb_upvote_section where username ='" + username + "' and word ='" + word + "' and section =" + section ;
            List<Map> result = getBySqlMapper.findRecords(sql);
            int status=0;
            if(result.size()>0){
                status =(Integer) result.get(0).get("status");

            }
            sql ="select count(*) from tb_upvote_section where status =1 and word ='" + word + "' and section =" + section ;

            int downvote = getBySqlMapper.findrows(sql);
            sql ="select count(*) from tb_upvote_section where status =2  and word ='" + word + "' and section =" + section ;
            int upvote = getBySqlMapper.findrows(sql);
            Map map =new HashMap();
            map.put("status",status);
            map.put("downvote",downvote);
            map.put("upvote",upvote);
            return EdatResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }



}
